/*****************************************************************************
 *   Copyright 2005 Tim A Wang
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *
 ****************************************************************************/
package com.jb.web.util;

import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class Mailer {
    /**
     * Helper method that sends a simple mail out using SMTP. This is only
     * a trivial implementation for the Requisition demo application.
     */
    public static String sendMail(String pfrom, String pto, String pcc, String pbcc, String psubject, String pmessage,
        String psmtpServer, boolean bHtml) {
        String lresult = "";

        // Name of the Host machine where the SMTP server is running
        String lhost = psmtpServer;

        // Gets the System properties
        Properties lprops = System.getProperties();

        // Puts the SMTP server name to properties object
        lprops.put("mail.smtp.host", lhost);

        // Get the default Session using Properties Object
        javax.mail.Session lsession = javax.mail.Session.getDefaultInstance(lprops, null);
        lsession.setDebug(false); // Disable the debug mode

        try {
            MimeMessage lmsg = new MimeMessage(lsession); // Create a New message
            lmsg.setFrom(new InternetAddress(pfrom)); // Set the From address

            // Setting the "To recipients" addresses
            lmsg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(pto, false));

            // Setting the "Cc recipients" addresses
            lmsg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(pcc, false));

            // Setting the "BCc recipients" addresses
            lmsg.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(pbcc, false));

            lmsg.setSubject(psubject); // Sets the Subject

            // Create and fill the first message part
            MimeBodyPart lmbp = new MimeBodyPart();

            if (bHtml) {
                lmbp.setDataHandler(new DataHandler(new ByteArrayDataSource(pmessage, "text/html")));
            } else {
                lmbp.setText(pmessage);
            }

            // Create the Multipart and its parts to it
            Multipart lmp = new MimeMultipart();
            lmp.addBodyPart(lmbp);

            // Add the Multipart to the message
            lmsg.setContent(lmp);

            // Set the Date: header
            lmsg.setSentDate(new Date());

            // Send the message
            Transport.send(lmsg);

            // If here, then message is successfully sent.
            // Display Success message
            lresult = lresult + "Mail was successfully sent to: " + pto + "\n";

            //if CCed then, add html for displaying info
            if (!pcc.equals("")) {
                lresult = lresult + "CCed To:" + pcc + "\n";
            }

            //if BCCed then, add html for displaying info
            if (!pbcc.equals("")) {
                lresult = lresult + "\n" + pbcc;
            }

            return lresult;
        } catch (MessagingException mex) {
            mex.printStackTrace();
        } finally {
        }

        return null;
    }
}
